package com.ita.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ita.util.RSAUtil;

public class CharFilter implements Filter {

	private static final boolean String = false;
	private String charset = "utf-8";
	private Logger logger  = Logger.getLogger(CharFilter.class);

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String charset = config.getInitParameter("chars");
		if (charset != null && !charset.isEmpty()) {
			this.charset = charset;
		}
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

//		System.out.println("char filter");
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp =(HttpServletResponse) response;
		
//		Boolean tag = false;
//		Cookie[] cookies = req.getCookies();
//		for(Cookie cookie : cookies) {
//			if("IP".equals(cookie.getName())){
//				String ip = cookie.getComment();
//				if(!getIpAddress(req).equals(RSAUtil.getInstance().decryt(ip))) {
//					return;
//				}else {
//					tag = true;
//				}
//			}	
//		}
//		
//		if(!tag) {
//			Cookie cookie = new Cookie("IP", RSAUtil.getInstance().encryt(getIpAddress(req)));
//			resp.addCookie(cookie);
//		}
		
		String ip = getIpAddress(req);
		
		//密钥加密
		Cookie cookie = new Cookie("IP", ip);
		
		resp.addCookie(cookie);
		
		Enumeration<String> headerNames = req.getHeaderNames();

		StringBuilder sb = new StringBuilder();
		while (headerNames.hasMoreElements()) {
			String headerName= headerNames.nextElement();
			sb.append(headerName+":");
			sb.append(req.getHeader(headerName)+";");
		}
		
		logger.info(sb.toString());
		
		if (req.getMethod().equalsIgnoreCase("GET")) {
			response.setCharacterEncoding(charset);
		} else {
			req.setCharacterEncoding(charset);//
		}
		chain.doFilter(req, response);

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	private String getIpAddress(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  

}
